package cn.nyse.ktc_problem.controller;
import java.util.Map;

import ktc.cn.ktc_base.entity.TbLabel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import cn.nyse.ktc_problem.entity.Problem;
import cn.nyse.ktc_problem.service.ProblemService;

import wfw.cn.ktc_common.entity.PageResult;
import wfw.cn.ktc_common.entity.Result;
import wfw.cn.ktc_common.entity.ResultCode;

/**
 * @Description 问题 控制器层
 * @author admin
 * @date 2020-10-12 17:47:06
*/
@RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController{

    @Autowired
    private ProblemService problemService;

    /**
    * 查询全部数据
    * @return
    */
    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(ResultCode.SUCCESS,problemService.findAll());
    }

    /**
    * 根据ID查询
    * @param id ID
    * @return
    */
    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(ResultCode.SUCCESS,problemService.findById(id));
    }

    /**
    * 分页+多条件查询
    * @param searchMap 查询条件封装
    * @param page 页码
    * @param size 页大小
    * @return 分页结果
    */
    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
        public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Problem> pageList = problemService.findSearch(searchMap, page, size);
        return  new Result(ResultCode.SUCCESS, new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent()) );
    }

    /**
    * 根据条件查询
    * @param searchMap
    * @return
    */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(ResultCode.SUCCESS, problemService.findSearch(searchMap));
    }

    /**
    * 增加
    * @param problem
    */
    @RequestMapping(method=RequestMethod.POST)
    public Result add(@RequestBody  Problem  problem  ){
        problemService.add( problem);
        return new Result(ResultCode.SUCCESS);
    }

    /**
    * 修改
    * @param problem
    */
    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@RequestBody  Problem  problem, @PathVariable String id ){
        problem.setId(id);
        problemService.update( problem);
        return new Result(ResultCode.SUCCESS);
    }

    /**
    * 删除
    * @param id
    */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        problemService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @PostMapping("all/{label}/{page}/{size}")
    public Result problePage(@RequestBody TbLabel tbLabel,@PathVariable("label") String label,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Problem> problemPage = problemService.findAll(PageRequest.of(page, size));
        return new Result(ResultCode.SUCCESS,new PageResult<Problem>(problemPage.getTotalElements(),problemPage.getContent()));
    }

    @GetMapping("newlist/{label}/{page}/{size}")
    public Result newProblePage(@RequestBody TbLabel tbLabel,@PathVariable("label") String label,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Problem> problemPage = problemService.findAll(PageRequest.of(page, size));
        return new Result(ResultCode.SUCCESS,new PageResult<Problem>(problemPage.getTotalElements(),problemPage.getContent()));
    }

    @GetMapping("hotlist/{label}/{page}/{size}")
    public Result hotProblePage(@RequestBody TbLabel tbLabel,@PathVariable("label") String label,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Problem> problemPage = problemService.findAll(PageRequest.of(page, size));
        return new Result(ResultCode.SUCCESS,new PageResult<Problem>(problemPage.getTotalElements(),problemPage.getContent()));
    }

    @GetMapping("waitlist/{label}/{page}/{size}")
    public Result waitProblePage(@RequestBody TbLabel tbLabel,@PathVariable("label") String label,@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Problem> problemPage = problemService.findAll(PageRequest.of(page, size));
        return new Result(ResultCode.SUCCESS,new PageResult<Problem>(problemPage.getTotalElements(),problemPage.getContent()));
    }
}
